package com.hairline.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hairline.bean.Brand;
import com.hairline.bean.BrandExample;
import com.hairline.bean.vo.WxBrandListVo;
import com.hairline.bean.vo.WxSearchIndexVo;
import com.hairline.mapper.BrandMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fenglin on 2020/9/4 8:30 PM
 */
@Service
public class WxBrandServiceImpl implements WxBrandService {

    @Autowired
    BrandMapper brandMapper;

    /**
     * 显示品牌制造商直供界面
     * @param page 当前页面
     * @param size 品牌制造商总数
     * @return
     */
    @Override
    public WxBrandListVo brandList(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        //先取出brand表中的全部数据，并计算总数total
        BrandExample example = new BrandExample();
        List<Brand> brandList = brandMapper.selectByExample(example);
        PageInfo<Brand> info = new PageInfo<>(brandList);
        long total = info.getTotal();
        List<Map<String, Object>> brandListLess = new ArrayList<>();
        //取出brandList中的特定几列
        for(Brand brand: brandList){
            Map<String, Object> map = new HashMap<>();
            map.put("id", brand.getId());
            map.put("name", brand.getName());
            map.put("desc", brand.getDesc());
            map.put("picUrl", brand.getPicUrl());
            map.put("floorPrice", brand.getFloorPrice());
            brandListLess.add(map);
        }
        //返回
        WxBrandListVo wxBrandListVo = new WxBrandListVo(total,brandListLess);
        return wxBrandListVo;
    }

    /**
     * 显示指定id的品牌制造商信息
     * @param id 品牌制造商id
     * @return
     */
    @Override
    public Brand brandDetail(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        return brand;
    }
}
